package foundation.StringUitl;

import java.util.ArrayList;
import java.util.List;

public class StringUtil {
	/**
	 * 将一个字符串的首字母变成大写字母
	 * 
	 * @param srcStr
	 *            ：源字符串
	 * @return：变换成首字母大写的字符串
	 */
	public static String toIniUpperCase(String srcStr) {
		String resultStr = null;

		if (srcStr != null) {
			resultStr = "";
			int len = srcStr.length();

			for (int i = 0; i < len; i++) {
				char ch = srcStr.charAt(i);
				if (ch == '\'')
					continue;
				if (i == 0 && Character.isLowerCase(ch))
					resultStr += Character.toUpperCase(ch);
				else
					resultStr += ch;
			}
		}
		return resultStr;
	}

	/**
	 * 将一个字符串的首字母变成小写字母
	 * 
	 * @param srcStr
	 *            ：源字符串
	 * @return：变换成首字母小写的字符串
	 */
	public static String toIniLowerCase(String srcStr) {
		String resultStr = null;

		if (srcStr != null) {
			resultStr = "";
			int len = srcStr.length();

			for (int i = 0; i < len; i++) {
				char ch = srcStr.charAt(i);
				if (ch == '\'')
					continue;
				if (i == 0 && Character.isUpperCase(ch))
					resultStr += Character.toLowerCase(ch);
				else
					resultStr += ch;

			}
		}
		return resultStr;
	}
	
	
	public static String toAllUpperCase(String srcStr) {
		String resultStr = null;

		if (srcStr != null) {
			resultStr = "";
			int len = srcStr.length();

			for (int i = 0; i < len; i++) {
				char ch = srcStr.charAt(i);
				if (ch == '\'')
					continue;
				if (Character.isLowerCase(ch))
					resultStr += Character.toUpperCase(ch);
				else
					resultStr += ch;
			}
		}
		return resultStr;
	}
	

	/**
	 * 逻辑事实解析成对应的字符串数据
	 * 
	 * @param fact
	 *            ：逻辑事实，例如synchcall(transmit,startProc_ph1,1).
	 * @return：逻辑事实对应的字符串数组[synchcall,transmit,startProc_ph1,1]
	 */
	public static List<String> getFactInfo(String fact) {
		List<String> list = new ArrayList<String>();
		String str[] = fact.split("\\(");
		String head = str[0];
		list.add(head);
		String bb = str[1];
		String strs[] = bb.split("\\)");
		String lqnm_strs[] = (strs[0]).split("\\,");
		for (int i = 0; i < lqnm_strs.length; i++) {
			list.add(lqnm_strs[i]);
		}
		return list;
	}

	/**
	 * 比较两个字符串对应的总ASC码(各位上字符ASC码值之和)
	 * 
	 * @param srcStr
	 * @param tgtStr
	 * @return：相等返回0,前者大于后者返回1,前者小于后者返回-1
	 */
	public static int compareASC(String srcStr, String tgtStr) {
		int result = 0;
		int srcASC = 0;
		int tgtASC = 0;
		byte[] srcBytes = srcStr.getBytes();
		int srcSize = srcBytes.length;
		for (int i = 0; i < srcSize; i++) {
			srcASC += (int) srcBytes[i];
		}
		byte[] tgtBytes = tgtStr.getBytes();
		int tgtSize = tgtBytes.length;
		for (int j = 0; j < tgtSize; j++) {
			tgtASC += (int) tgtBytes[j];
		}

		if (srcASC - tgtASC > 0) {
			result = 1;
		}

		if (srcASC - tgtASC < 0) {
			result = -1;
		}

		return result;
	}

	public static void main(String[] args) {
		System.out.println(toAllUpperCase("s1a2b3c"));
		System.out.println(compareASC("12", "123"));
		System.out.println(compareASC("a", "b"));
	}
}
